package com.yanqu.road.server.cmd.tradewar.cross;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.cross.tradewar.CrossTradeWarProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.TradeWarActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;


@Cmd(code = Protocol.S_CROSS_TRADE_WAR_SIMPLE_NOTICE, desc = "跨服商战简略公告")
public class CrossTradeWarSimpleNoticeCmd extends AbstractCommand {
	private int activityId;
	private long userId;
	private long serverId;
	private long enemyUserId;
	private long enemyServerId;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		CrossTradeWarProto.CrossTradeWarSimpleNoticeReqMsg reqMsg = CrossTradeWarProto.CrossTradeWarSimpleNoticeReqMsg.parseFrom(packet.getBytes());
		activityId = reqMsg.getActivityId();
		userId = reqMsg.getUserId();
		serverId = reqMsg.getServerId();
		enemyUserId = reqMsg.getEnemyUserId();
		enemyServerId = reqMsg.getEnemyServerId();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		ActivityInfo activityInfo = TradeWarActivityMgr.getOpenActivityInfo(activityId);
		if(null == activityInfo){
			ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
			return;
		}
		//发送到跨服
		CrossTradeWarProto.CrossTradeWarSimpleNoticeReqMsg.Builder reqMsg = CrossTradeWarProto.CrossTradeWarSimpleNoticeReqMsg.newBuilder();
		reqMsg.setActivityId(activityId);
		reqMsg.setUserId(userId);
		reqMsg.setServerId(serverId);
		reqMsg.setEnemyUserId(enemyUserId);
		reqMsg.setEnemyServerId(enemyServerId);
		player.sendUnionActivityGroupPacket(CrossProtocol.C_CROSS_TRADE_WAR_SIMPLE_NOTICE, reqMsg, activityId);
	}

	@Override
	public void sendData(GamePlayer player) {
		if(0 != ret) {
			CrossTradeWarProto.CrossTradeWarSimpleNoticeRespMsg.Builder respMsg = CrossTradeWarProto.CrossTradeWarSimpleNoticeRespMsg.newBuilder();
			respMsg.setRet(ret);
			player.sendPacket(Protocol.U_CROSS_TRADE_WAR_SIMPLE_NOTICE, respMsg);
		}
	}
}
